﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
using VCN.App.DataTransferObjects;

namespace VCN.admin
{
    public partial class RelativeProductManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                this.LoadDataToDdl();
               // this.LoadDataToDdlProduct();
                this.ReloadUserData();
                Page.Title = "Sửa sản phẩm liên quan";
            }
        }
        private void LoadDataToDdl()
        {
            CategoriesDAO v_CategoryDAO = new CategoriesDAO();
            DataTable v_dt = v_CategoryDAO.selectAll2Dt();
            ddl_Category.DataSource = v_dt;
            ddl_Category.DataTextField = "CATEGORY_NAME";
            ddl_Category.DataValueField = "Id";
            ddl_Category.DataBind();
        }
        private void LoadDataToDdlProduct()
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            DataTable v_dt = v_ProductDAO.SelectByCategoryId(int.Parse(ddl_Category.SelectedValue));
            ddl_Product.DataSource = v_dt;
            ddl_Product.DataTextField = "PRODUCT_NAME";
            ddl_Product.DataValueField = "Id";
            ddl_Product.DataBind();
        }

        private void ReloadUserData()
        {
            string v_id = Request.QueryString["Id"].ToString();
            if (!String.IsNullOrEmpty(v_id))
            {
                RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
                RelativeProduct v_RelativeProduct = new RelativeProduct();
                ProductDAO v_ProductDAO = new ProductDAO();
                v_RelativeProduct = v_RelativeProductDAO.SelectById(int.Parse(v_id));
                imgPreview.AlternateText = v_RelativeProduct.Image;
                m_txt_RelativeProductName.Text = v_RelativeProduct.Name;
                imgPreview.ImageUrl = string.Format("../Images/Product/{0}", v_RelativeProduct.Image);
                try
                {
                    ddl_Category.SelectedValue = v_ProductDAO.SelectById(v_RelativeProduct.ProductId).CateId.ToString();
                    LoadDataToDdlProduct();
                    ddl_Product.SelectedValue = v_RelativeProduct.ProductId.ToString();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        }
        protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataToDdlProduct();
        }
        protected void btnAdd_click(object sender, EventArgs e)
        {
            RelativeProduct v_RelativeProduct = new RelativeProduct();
            v_RelativeProduct.Id = int.Parse(Request.QueryString["Id"].ToString());
            v_RelativeProduct.ProductId = int.Parse(ddl_Product.SelectedItem.Value);
            v_RelativeProduct.Image = this.doUploadPImage();
            v_RelativeProduct.Name = m_txt_RelativeProductName.Text;
            RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
            v_RelativeProductDAO.update(v_RelativeProduct);
            Response.Redirect("RelativeList.aspx");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_click(object sender, EventArgs e)
        {
            Response.Redirect("RelativeList.aspx");
        }
        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../Images/Product/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
                return FileUpload.FileName;
            }
            return this.imgPreview.AlternateText;
        }
    }
}
